package com.zzt.zt_gson;

import com.google.gson.Gson;
import com.zzt.gson.util.GsonUtil;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by lin on 2018/8/29.
 */
public class HttpResponse<T> implements Serializable {

    private boolean success;
    private String errorCode;
    private String errorInfo;
    private String AccessKeyId;//阿里云
    private String AccessKeySecret;//阿里云
    private String Expiration;//阿里云
    private String SecurityToken;//阿里云
    private int StatusCode; //200 为成功，其他就默认错误
    private T data;

    public String getAccessKeyId() {
        return AccessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        AccessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return AccessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        AccessKeySecret = accessKeySecret;
    }

    public String getExpiration() {
        return Expiration;
    }

    public void setExpiration(String expiration) {
        Expiration = expiration;
    }

    public String getSecurityToken() {
        return SecurityToken;
    }

    public void setSecurityToken(String securityToken) {
        SecurityToken = securityToken;
    }

    public int getStatusCode() {
        return StatusCode;
    }

    public void setStatusCode(int statusCode) {
        StatusCode = statusCode;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorInfo() {
        return errorInfo;
    }

    public void setErrorInfo(String errorInfo) {
        this.errorInfo = errorInfo;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static HttpResponse fromJson(String json, Type type) {
//        Gson gson = new Gson();
        Gson gson = GsonUtil.getInstance().getGson();
        Type objectType = type(HttpResponse.class, type);
        return gson.fromJson(json, objectType);
    }

    static ParameterizedType type(final Class raw, final Type... args) {
        return new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
    }

    @Override
    public String toString() {
        return "HttpResponse{" +
                "success=" + success +
                ", errorCode='" + errorCode + '\'' +
                ", errorInfo='" + errorInfo + '\'' +
                ", AccessKeyId='" + AccessKeyId + '\'' +
                ", AccessKeySecret='" + AccessKeySecret + '\'' +
                ", Expiration='" + Expiration + '\'' +
                ", SecurityToken='" + SecurityToken + '\'' +
                ", StatusCode=" + StatusCode +
                ", data=" + data +
                '}';
    }
}
